package com.lening.provider.controller.PropertyNotice;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lening.common.base.BaseController;
import com.lening.common.excel.ExcelUtils;
import com.lening.provider.dto.PropertyNotice.PropertyNotificationMainTableDTO;
import com.lening.provider.po.PropertyNotice.PropertyNotificationMainTablePO;
import com.lening.provider.service.PropertyNotice.PropertyNotificationMainTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.reactive.function.client.WebClient;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘政华
 * @datetime 2024/6/5 14:07
 * @description 类对象
 */
@Slf4j
@RestController
@RequestMapping("propertyNotificationMainTables")
public class PropertyNotificationMainTableController extends BaseController<PropertyNotificationMainTablePO, PropertyNotificationMainTableDTO, PropertyNotificationMainTableService> {

    @Autowired(required = false)
    private WebClient webClient;

    @Autowired
    private PropertyNotificationMainTableService propertyNotificationMainTableService;

    @PostMapping("/import")
    public void importUser(@RequestPart("file") MultipartFile file) throws Exception {
        List<PropertyNotificationMainTablePO> property = ExcelUtils.readMultipartFile(file, PropertyNotificationMainTablePO.class);
        ArrayList<PropertyNotificationMainTablePO> arrayList = new ArrayList<>();
        for (int i = 0; i < property.size(); i++) {
            arrayList.add(property.get(i));
            if (arrayList.size()>=100){
                propertyNotificationMainTableService.saveBatch(arrayList);
                arrayList.clear();
            }
        }

        propertyNotificationMainTableService.saveBatch(arrayList);
        System.out.println("剩余数据"+arrayList);

    }

    @GetMapping("export/{id}")
    public void export(@PathVariable Long id, HttpServletResponse response) {
        // 假设 ownerRepairMainTableService 有一个根据 id 获取数据的方法
        PropertyNotificationMainTablePO propertyNotificationMainTablePO = propertyNotificationMainTableService.getById(id);

        // 如果只有一个对象要导出，你可能想将其放入一个列表中
        List<PropertyNotificationMainTablePO> list = new ArrayList<>();
        if (propertyNotificationMainTablePO != null) {
            list.add(propertyNotificationMainTablePO);
        }

        // 现在，你可能需要修改 ExcelUtils.export 方法来处理只有一个对象的情况
        // 或者，如果它已经能够处理空列表或单个对象，那么可以直接调用它
        ExcelUtils.export(response, "物业通知表", list, PropertyNotificationMainTablePO.class);
    }

    //工作台今日提醒
    @GetMapping("getListTwo")
    public Page<PropertyNotificationMainTableDTO> getListTwo(PropertyNotificationMainTableDTO propertyNotificationMainTableDTO) {
        return this.propertyNotificationMainTableService.getlistTwo(propertyNotificationMainTableDTO);
    }


}
